<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{margin:0;padding: 0;}
        #parent{
            border:1px solid #000;
            width: 440px;
        }
        img{
            width: 400px;
            padding: 20px;
        }
        #fade{
            padding: 20px;
            width: 400px;
            height: 140px;
            position: absolute;
            background: red;
            opacity: 0.3;
            left: 0;
            top: 0;
        }
    </style>
    
</head>
<body>
    <div id="parent">
        <img src="images/01.jpg" alt="">
        <div id="fade"></div>
    </div>
    <script>
        var parent = document.getElementById("parent");
        var fade = document.getElementById("fade");
        var opacity = getComputedStyle(fade).opacity*100;
        var timer;
        parent.onmouseover = function(){
            clearInterval(timer);//如果不清除，鼠标不停进出的时候它颜色变深的速度会加快
            setInterval(function(){
                if(opacity>70){
                    clearInterval(timer);
                }
                else{
                    opacity += 2;
                    fade.style.opacity = opacity/100;
                }
                
            },50)
        }
        parent.onmouseout = function(){
            clearInterval(timer);
            timer = setInterval(function(){
                if(opacity<30){
                    clearInterval(timer);
                }
                else{
                    opacity -= 2;
                    fade.style.opacity = opacity/100;
                }
            },50)
        }
    </script>
</body>
</html>